package classes {
	import flash.utils.ByteArray;
	import flash.net.Socket;
	import flash.events.ProgressEvent;
	import mx.controls.Alert;
	
	public class SmtpSocket extends Socket{
		public var Host:String;
		public var Port:int;
		public var Username:String;
		public var Password:String;
		public var loggedIn:Boolean;
		
		private static var Bit64:Array = new Array(
			'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
			'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
			'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
			'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
		);

		public function Base64Enc(s:String):String{
			var buf:Array = new Array(3);
			var rtn:String = "";
			var x:int = 0;
			var y:int = s.length;
			var z:int;
			
			while(x < y){
				for(z=0; z < 3; z++){
					buf[z] = (x < y)? s.charCodeAt(x) : -1;
					x++
				}//for
				
				rtn += Bit64[ buf[0] >> 2];
				if(buf[1] != -1){
					rtn += ( Bit64[ ((buf[0] << 4) & 0x30) | (buf[1] >> 4) ]);
					if(buf[2] != -1){
						rtn += ( Bit64[ ((buf[ 1 ] << 2) & 0x3c) | (buf[ 2 ] >> 6) ]);
						rtn += Bit64[buf[ 2 ] & 0x3F];
					}else{
						rtn += Bit64[((buf[ 1 ] << 2) & 0x3c )];
						rtn += "=";
					}//if
				}else{
					rtn += Bit64[((buf[ 0 ] << 4 ) & 0x30 )];
					rtn += "=";
					rtn += "=";
				}//if
			}//while

			return rtn;
		}//func

		public function SmtpSocket(){
			addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
		}//constructor
		
		public function SendCommand(str:String):void{
			this.writeUTFBytes(str + "\r\n");
			this.flush();
		}//func
		
		private function onSocketData ( pEvt:ProgressEvent ):void{
			var response:String = pEvt.target.readUTFBytes ( pEvt.target.bytesAvailable );
			var reg:RegExp = /^(\d{3})\s([^\r\n]+)/img;
			var result:Object = reg.exec(response);
			
			var code:String = "";
			var msg:String = "";
			
			if(result != null){
				code = result[1];
				msg = result[2];
			}//if

			trace("SocketDataCode : " + code);
			trace("SocketData : " + response + "+.+");
			//trace("SocketMsg: " + msg + "+.+");

			if(code == "220"){ //Service Ready,Connected
				SendCommand("EHLO [192.168.168.10]"); //Send HandShake
			} else if(code == "250" && loggedIn == false) { 
				SendCommand("AUTH LOGIN");
				//trace("login");
			} else if(code == "250") { 
				
			} else if(code == "334"){
				if(msg == "VXNlcm5hbWU6"){ //Username:
					//trace(Username);
					SendCommand(Base64Enc(Username));
				}else if(msg == "UGFzc3dvcmQ6") { //Password:
					//trace(Password);
					SendCommand(Base64Enc(Password)); //Password
				}//
			}else if(code == "535") {
				//trace("Authentication unsuccessful");
			}else if(code == "235"){ //Authentication successful
				//trace("account good");
				loggedIn = true;
				this.writeUTFBytes("MAIL FROM: <tdownes@sanative.net>\r\n");
				this.writeUTFBytes("RCPT TO: <jonin@ninjamaster.com>\r\n");
				this.writeUTFBytes("DATA\r\n");
				this.writeUTFBytes("Subject: Test Subject\r\n");
				this.writeUTFBytes("Mime-Version: 1.0\r\n");
				this.writeUTFBytes("Content-Type: text/html; charset=UTF-8; format=flowed\r\n");
				this.writeUTFBytes("HELLO<br><b>testing</b>\r\n");
				this.writeUTFBytes(".\r\n");
				this.flush();
			}//if

		}//
		
	}//class

}//package